package com.zb.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjSystemServiceLog;
import com.zb.entity.ZjSystemServiceLogAnnex;
import com.zb.mapper.ZjSystemServiceLogMapper;
import com.zb.service.ZjSystemServiceLogAnnexService;
import com.zb.service.ZjSystemServiceLogService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统服务商日志表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-07
 */
@Log4j2
@Service
public class ZjSystemServiceLogServiceImpl extends ServiceImpl<ZjSystemServiceLogMapper, ZjSystemServiceLog> implements ZjSystemServiceLogService {
    @Autowired
    private ZjSystemServiceLogAnnexService systemServiceLogAnnexService;

    // 新增系统服务日志
    @Override
    public void save(Long custAccId, String type, String remark, List<String> annexs) {
        try {
            ZjSystemServiceLog log = new ZjSystemServiceLog();
            log.setId(IdWorker.getId());
            log.setCustAccId(custAccId);
            log.setType(type);
            log.setRemark(remark);
            log.setCreateEmp("系统服务商");
            this.save(log);
            systemServiceLogAnnexService.save(log.getId(), custAccId, annexs);
        }catch (Exception e) {
            log.error("新增系统服务日志异常：{}", e);
        }
    }

    // 新增系统服务日志
    @Override
    public void save(List<Long> custAccIdList, String type, String remark, List<String> annexs) {
        if (null == custAccIdList || custAccIdList.isEmpty()) {
            return;
        }
        try {
            List<ZjSystemServiceLog> logs = new ArrayList<>();
            ZjSystemServiceLog log = null;
            List<ZjSystemServiceLogAnnex> logAnnexes = new ArrayList<>();
            ZjSystemServiceLogAnnex an = null;
            boolean hasAnnex = null != annexs && !annexs.isEmpty();
            for (Long custAccId : custAccIdList) {
                log = new ZjSystemServiceLog();
                log.setId(IdWorker.getId());
                log.setCustAccId(custAccId);
                log.setType(type);
                log.setRemark(remark);
                log.setCreateEmp("系统服务商");
                logs.add(log);

                if (hasAnnex) {
                    logAnnexes.clear();
                    for (String s : annexs) {
                        an = new ZjSystemServiceLogAnnex();
                        an.setId(log.getId());
                        an.setCustAccId(custAccId);
                        an.setImage(s);
                        logAnnexes.add(an);
                    }
                }
            }
            this.saveBatch(logs);
            if (hasAnnex) {
                systemServiceLogAnnexService.saveBatch(logAnnexes);
            }
        }catch (Exception e) {
            log.error("新增系统服务日志异常：{}", e);
        }
    }
}
